Skip to content

(Fixes #420) Fix false positive in no-side-effect-in-cp #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

michalsnik
Copy link
Member

This PR fixes #420, by introducing parser that simplifies given MemberExpression or CallExpression to version that can be properly verified in terms of potential side-effect in the chain.

So for example this kind of code:

this.categories['test']
  .map(c => c.items)
  .sort()[0]
  .anotherProp
  .slice(1)
  .reduce((acc, item) => {
    // item.push(10)
    return acc + item.length;
  }, 0)
  .length

Is being simplified to this:

this.categories.test.map().sort()[].anotherProp.slice().reduce().length

And now we can properly check for potential side-effects in the given chain without potential false-positives hidden in inner scopes.

@michalsnik michalsnik self-assigned this Apr 21, 2018
Copy link
Contributor

@chrisvfritz chrisvfritz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a nice improvement! I don't think it necessarily needs to be part of this PR, but playing around I tried adding a few more test cases and I noticed test8 and test9 below don't current trigger any warnings.

diff --git a/tests/lib/rules/no-side-effects-in-computed-properties.js b/tests/lib/rules/no-side-effects-in-computed-properties.js
index 3eadc45..b8088b8 100644
--- a/tests/lib/rules/no-side-effects-in-computed-properties.js
+++ b/tests/lib/rules/no-side-effects-in-computed-properties.js
@@ -173,6 +173,22 @@ ruleTester.run('no-side-effects-in-computed-properties', rule, {
           },
           test6() {
             return this.something.keys.sort()
+          },
+          test7() {
+            this.types.forEach(c => {
+              this.something[c.category] = c
+            })
+            return {}
+          },
+          test8() {
+            this.types.forEach(c => {
+              globalVar = c
+            })
+            return {}
+          },
+          test9() {
+            globalVar = this.something
+            return {}
           }
         }
       })`,
@@ -198,6 +214,15 @@ ruleTester.run('no-side-effects-in-computed-properties', rule, {
       }, {
         line: 25,
         message: 'Unexpected side effect in "test6" computed property.'
+      }, {
+        line: 29,
+        message: 'Unexpected side effect in "test7" computed property.'
+      }, {
+        line: 35,
+        message: 'Unexpected side effect in "test8" computed property.'
+      }, {
+        line: 40,
+        message: 'Unexpected side effect in "test9" computed property.'
       }]
     },
     {

If it's easy to fix, it could be in this PR. Otherwise, maybe a separate one. What do you think?

@michalsnik
Copy link
Member Author

Let's do it in separate PR, you can fire an issue to discuss it @chrisvfritz. Although everything about this rule is a balance between being useful and not full of false positives. But we can discuss ofc :)

@michalsnik michalsnik merged commit 409fe31 into master Jul 11, 2018
@michalsnik michalsnik deleted the fix-no-side-effect-in-computed-property-false-positive branch July 11, 2018 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive in vue/no-side-effects-in-computed-properties
2 participants